<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"   import="pojo.*,service.*,base.*,java.sql.Timestamp,java.text.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
    request.setCharacterEncoding("utf-8");
    
    if(session.getAttribute("date")!=null){
    int userId=(Integer)session.getAttribute("id");
    String date = session.getAttribute("date").toString();
	Timestamp checkinTime = Timestamp.valueOf(session.getAttribute("date").toString()+" 00:00:00");  
	 
	int type = (Integer)session.getAttribute("type");
	int tablearea = (Integer)session.getAttribute("tablearea");
	int tabletype = (Integer)session.getAttribute("tabletype");
	TableService ts = new TableService();
	
	int q = ts.getTotalTable(tablearea,tabletype);
	
	int m = ts.getTableNum(date,tablearea,tabletype,type);
	
	if(m<q){
	int memberId= (Integer)session.getAttribute("id");
	Timestamp time=new Timestamp(System.currentTimeMillis());
	String customerName=request.getParameter("realname");
	String phone = request.getParameter("tel");
	String IDcard= request.getParameter("IDcard");
	CartService cs = new CartService ();
	int totalNum=cs.getALL(userId);
	float totalPrice=Float.parseFloat(request.getParameter("totalprice"));
	OrderService os = new OrderService();
	String code = os.getOrderNo(userId);
	order postOrder=new order();

    postOrder.setTime(time);
	postOrder.setCoad(code);
	postOrder.setMemberId(memberId);
	postOrder.setCustomerName(customerName);
	postOrder.setCheckinTime(checkinTime);
	postOrder.setType(type);
	postOrder.setTableArea(tablearea);
	postOrder.setTableType(tabletype);
	postOrder.setTotalNum(totalNum);
	postOrder.setTotalPrice(totalPrice);
	postOrder.setPhone(phone);
	postOrder.setIDcard(IDcard);
	int r = os.postOrder(postOrder);
	String []ids=request.getParameterValues("id");
	for(int i=0;i<ids.length;i++){
	  orderDetail detail=new orderDetail();
	  DishesService ds = new DishesService();
	  CartService cart=new CartService();
	  detail.setOrderId(r);
	  detail.setDishesName(ds.getDishesDetail(cs.getCart(Integer.parseInt(ids[i])).getDishesId()).getName());
	  detail.setPrice(ds.getDishesDetail(cs.getCart(Integer.parseInt(ids[i])).getDishesId()).getPrice());
	  detail.setNumber(Integer.parseInt(request.getParameter("ordernum")));
		
	  os.add(detail,r); 
	  cs.deleteCart(Integer.parseInt(ids[i]));
	  
	 }
	 session.setAttribute("code",code);
	  session.setAttribute("orderid",r);
	  
	  response.sendRedirect("../../website/orderSuccess.jsp");
	}
	 else {out.print("您选定餐桌已被预订，请重新选择餐桌！！！");
                response.setHeader("refresh","1;URL=../../website/index.jsp") ;}
	
	
	
	
}


else {
                out.print("您还未选定餐桌，请选择餐桌！！！");
                response.setHeader("refresh","1;URL=../../website/index.jsp") ;
                
}

%>
</body>
</html>
